package van.planifolia.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/14 19:53
 */
@Service("testService")
public class TestService {
    @Resource(name = "bookService")
    private BookService bookService;

    @Transactional(
            // 隔离级别为可重复度
            isolation = Isolation.REPEATABLE_READ,
            // 传播机制为必须为新事务
            propagation = Propagation.REQUIRES_NEW,
            // 出现运行时异常，io异常，Sql异常回滚
            rollbackFor = {RuntimeException.class, IOException.class, SQLException.class},
            // 空指针异常不回滚
            noRollbackFor = {NullPointerException.class},
            // 超时时间为2s
            timeout = 2
    )
    public void byBooks(List<String> isbns, String username) {
        for (String isbn : isbns) {
            bookService.buyBook(username,isbn);
        }
    }
}
